<%@ page language="java" contentType="text/javascript;charset=UTF-8" pageEncoding="UTF-8"%>
<%out.clear();%>
function clickDGRowButNotChangeSelected(listDisplayEId,rowIndex,onClickFun){
	var isSelected = $('#'+listDisplayEId).datagrid('isSelectedRowIndex',rowIndex);
	if(!$('#'+listDisplayEId).datagrid('options').notSelectByRow){
		if(!isSelected){
			$('#'+listDisplayEId).datagrid('selectRow',rowIndex);
		}else{
			$('#'+listDisplayEId).datagrid('unselectRow',rowIndex);
		}
	}
	if(onClickFun){
		onClickFun();
	}
}

function formatDGField(value,row,index,field){
	if(!field){
		field = this.field;
	}
	var fields = field.split('.');
	var fieldValue = row;
	for(var i=0;i < fields.length;i++){
		fieldValue = fieldValue[fields[i]];
	}
	return fieldValue;
}

function formatDGAltField(value,row,index,altField){
	if(!altField){
		altField = this.altField;
	}
	var fields = altField.split('.');
	var fieldValue = row;
	for(var i=0;i < fields.length;i++){
		fieldValue = fieldValue[fields[i]];
	}
	return fieldValue;
}

function formatDGFieldDate(value,row,index,field){
	if(!field){
		field = this.field;
	}
	var fieldValue = formatDGField(value,row,index,field);
	if(fieldValue && fieldValue!=''){
		fieldValue = (fieldValue+'').split(' ')[0];
	}
	return fieldValue;
}

function formatDGFieldDatetime(value,row,index,field){
	if(!field){
		field = this.field;
	}
	var fieldValue = formatDGField(value,row,index,field);
	if(fieldValue && fieldValue!=''){
		fieldValue = fieldValue.split('.')[0];
	}
	return fieldValue;
}

function formatDGFieldToViewById(listDisplayEId,value,row,index,field,toViewUrl){
	if(!field){
		field = this.field;
	}
	var fieldValue = formatDGField(value,row,index,field);
	var idField = $("#"+listDisplayEId).datagrid("options").idField;
	var idValue = row[idField];
	if(toViewUrl.indexOf('?')<0){
		toViewUrl += '?';
	}
	var openWindowUrl = toViewUrl+'&'+idField+'='+idValue;
	fieldValue = '<a href="javascript:function onHrefA(){clickDGRowButNotChangeSelected(\''+listDisplayEId+'\','+index+',function(){window.open(\''+openWindowUrl+'\')});}onHrefA();">'+fieldValue+'<a/>';
	return fieldValue;
}

function searchDGData(searchFormEId,listDisplayEId){

	var searchFormE = document.getElementById(searchFormEId);
	var url = searchFormE.action;
	var paramStr = '';
	for(var i=0;i < searchFormE.elements.length;i++){
		var fieldE = searchFormE.elements[i];
		if(fieldE.name && ''!=fieldE.name){
			if(fieldE.type=='select-multiple'){
				for(var j=0;j<fieldE.options.length;j++){
					var optionE = fieldE.options[j];
					if(optionE.selected){
						paramStr = paramStr + '&' + fieldE.name + '=' + optionE.value;
					}
				}
			}else if(fieldE.type=='checkbox' || fieldE.type=='radio'){
				if(fieldE.checked){
					paramStr = paramStr + '&' + fieldE.name + '=' + fieldE.value;
				}
			}else{
				paramStr = paramStr + '&' + fieldE.name + '=' + fieldE.value;
			}
		}
	}
	$('#'+listDisplayEId).datagrid("options").queryParams=paramStr;
	$('#'+listDisplayEId).datagrid("options").url = url;
	$('#'+listDisplayEId).datagrid("load");
	
	return false;
}

function defaultDGAddFun(listDisplayEId,toAddUrl,dh,dw){
	window.showModalDialog(toAddUrl,{windowO:window,dgListDisplayEId:listDisplayEId},'dialogHeight:'+dh+'px;,dialogWidth:'+dw+'px');
}

function defaultDGEditFun(listDisplayEId,toEditUrl,dh,dw,otherParams){
	var ids = [];
	var rows = $('#'+listDisplayEId).datagrid('getSelections');
	
	otherParams = otherParams?otherParams:{};
	otherParams.altTitle = otherParams.altTitle?otherParams.altTitle:'提示';
	otherParams.altMsg = otherParams.altMsg?otherParams.altMsg:'请先选择一条，再进行修改！';
	
	if(rows.length == 0 || rows.length > 1){
		$.messager.alert(otherParams.altTitle, otherParams.altMsg, 'info');
	}else{
		if(toEditUrl.indexOf('?')<0){
			toEditUrl += '?';
		}
		var idField = $('#'+listDisplayEId).datagrid('options').idField;
		window.showModalDialog(toEditUrl+'&'+idField+'='+rows[0][idField],{windowO:window,dgListDisplayEId:listDisplayEId},'dialogHeight:'+dh+'px;,dialogWidth:'+dw+'px');
	}
}

function defaultDGDeleteFun(listDisplayEId,idsFieldName,delUrl,otherParams){
	var ids = [];
	var rows = $('#'+listDisplayEId).datagrid('getSelections');
	var idField = $('#'+listDisplayEId).datagrid('options').idField;
	for(var i=0;i < rows.length;i++){
		ids.push(rows[i][idField]);
	}
	
	otherParams = otherParams?otherParams:{};
	otherParams.altTitle = otherParams.altTitle?otherParams.altTitle:'提示';
	otherParams.altMsg = otherParams.altMsg?otherParams.altMsg:'请先选择，再进行删除！';
	otherParams.cfmTitle = otherParams.cfmTitle?otherParams.cfmTitle:'注意';
	otherParams.cfmMsg = otherParams.cfmMsg?otherParams.cfmMsg:'确定要删除所选记录吗？';
	otherParams.failureTitle = otherParams.failureTitle?otherParams.failureTitle:'错误';
	otherParams.failureMsg = otherParams.failureMsg?otherParams.failureMsg:'删除失败请与管理员联系！';
	otherParams.successTitle = otherParams.successTitle?otherParams.successTitle:'提示';
	otherParams.successMsg = otherParams.successMsg?otherParams.successMsg:'删除成功！';
	otherParams.errorTitle = otherParams.errorTitle?otherParams.errorTitle:'错误';
	otherParams.errorMsg = otherParams.errorMsg?otherParams.errorMsg:'请求删除失败请与管理员联系！';
	
	if(ids.length == 0){
		$.messager.alert(otherParams.altTitle,otherParams.altMsg,'info');
	}else{
		$.messager.confirm(otherParams.cfmTitle, otherParams.cfmMsg, function(r){
			var paramStr = '&'+idsFieldName+'='+ids.join('&'+idsFieldName+'=')
			if (r){
				$.ajax({type:'post',url:delUrl,data:paramStr,dataType:"json"
					,success:function(data){
						if(data.failure && data.failure==true){
							$.messager.alert(otherParams.failureTitle, otherParams.failureMsg, 'error');
						}else{
							$.messager.alert(otherParams.successTitle, otherParams.successMsg,'info', function(){
								$('#'+listDisplayEId).datagrid('clearSelections');
								$('#'+listDisplayEId).datagrid('reload');
							});
						}
					}
					,error:function(){
						$.messager.alert(otherParams.errorTitle, otherParams.errorMsg, 'error');
					}
				});
			}
		});
	}
}

if ($.fn.datebox){
	$.fn.datebox.defaults.formatter = function(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
	};
	$.fn.datebox.defaults.parser = function(s){
		if (!s) return new Date();
		var ss = s.split('-');
		var y = parseInt(ss[0],10);
		var m = parseInt(ss[1],10);
		var d = parseInt(ss[2],10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			return new Date(y,m-1,d);
		} else {
			return new Date();
		}
	};
}